<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="名称" prop="name">
        <el-input style="width:620px;"  v-model="dataForm.name" placeholder="服务器名称"></el-input>
      </el-form-item>
      <div>
      <el-form-item style="float: left;" label="所属服务">
          <el-select style="width:250px;" v-model="dataForm.backendId" placeholder="请选择">
             <el-option v-for="( b,index) in backends" :label="b.name" :value="b.id"></el-option>
           </el-select>
      </el-form-item>
      <el-form-item  style="float: left;" label="模块类型">
          <el-select style="width:250px;" v-model="dataForm.moduleType" placeholder="请选择">
             <el-option label="业务模块[Springboot]" value="springboot"></el-option>
             <el-option label="网关[Gateway]" value="gateway"></el-option>
             <el-option label="注册中心[Eureka]" value="eureka"></el-option>
              <el-option label="War[Tomcat]" value="tomcat"></el-option>
           </el-select>
      </el-form-item>
      <div style="clear:both"></div>
      </div>
      <el-form-item  label="服务器">
          <el-select style="width:620px;" v-model="dataForm.serverId" placeholder="请选择">
             <el-option v-for="( s,index) in servers" :label="s.name+'   【'+s.ipAddr+'】'" :value="s.id"></el-option>
           </el-select>
      </el-form-item>
      <el-form-item  label="部署路径">
          <el-input style="width:620px;"  v-model="dataForm.deployPath" placeholder="部署路径"></el-input>
      </el-form-item>
      <el-form-item  label="部署包名称">
          <el-input style="width:620px;"  v-model="dataForm.nameJar" placeholder="部署包名称"></el-input>
      </el-form-item>
      <div>
          <el-form-item style="float:left;"  label="数据库服务">
              <el-select style="width:250px;" v-model="dataForm.databaseId" placeholder="请选择">
                 <el-option label="未使用" :value="0"></el-option>
                 <el-option v-for="( ds,index) in datasources" v-if="ds.datasourceType=='mysql'" :label="ds.name+'   【'+ds.serverIpaddr+'】'" :value="ds.id"></el-option>
               </el-select>
          </el-form-item>
           <el-form-item  style="float:left;"  label="数据库名称">
               <el-input style="width:250px;"  v-model="dataForm.dbName" placeholder="库名称"></el-input>
           </el-form-item>
      <div style="clear:both"></div>
      </div>
      <div>
        <el-form-item style="float:left;"  label="数据仓库">
            <el-select style="width:250px;" v-model="dataForm.mongodbId" placeholder="请选择">
                <el-option label="未使用" :value="0"></el-option>
                <el-option v-for="( ds,index) in datasources" v-if="ds.datasourceType=='mongodb'" :label="ds.name+'   【'+ds.serverIpaddr+'】'" :value="ds.id"></el-option>
             </el-select>
        </el-form-item>
        <el-form-item style="float:left;"  label="库名称">
            <el-input style="width:250px;"  v-model="dataForm.mondbName" placeholder="库名称"></el-input>
        </el-form-item>
        <div style="clear:both"></div>
      </div>
       <div>
          <el-form-item  style="float:left;" label="缓存">
          <el-select style="width:250px;" v-model="dataForm.redisId" placeholder="请选择">
              <el-option label="未使用" :value="0"></el-option>
              <el-option v-for="( ds,index) in datasources" v-if="ds.datasourceType=='redis'" :label="ds.name+'   【'+ds.serverIpaddr+'】'" :value="ds.id"></el-option>
           </el-select>
          </el-form-item>
         <el-form-item style="float:left;"  label="库名称">
              <el-input style="width:250px;"  v-model="dataForm.redbName" placeholder="库名称"></el-input>
          </el-form-item>
        <div style="clear:both"></div>
      </div>
      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isEmail, isMobile } from '@/utils/validate'
  export default {
    data () {
      return {
        visible: false,
        projects: [],
        backends:[],
        servers:[],
        datasources:[],
        dataForm: {
          id: 0,
          name: '',
          status: 1,

          backendId: '',
          moduleType: '',
          serverId: '',
          deployPath: '',
          nameJar:'',
          databaseId:'', mongodbId:'', redisId:'',
          dbName:'',mondbName:'',redbName:'',
        },
        dataRule: {
          name: [
            { required: true, message: '服务器名称不能为空', trigger: 'blur' }
          ],

          ipAddr: [
            { required: true, message: 'IP地址不能为空', trigger: 'blur' },
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl(`/project/all`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.projects = data.data
          }
        })
        this.$http({
          url: this.$http.adornUrl(`/backend/all`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.backends = data.data
          }
        })
        this.$http({
          url: this.$http.adornUrl(`/systemconfig/servers/all`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.servers = data.data
          }
        })
        this.$http({
          url: this.$http.adornUrl(`/datasource/all`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.datasources = data.data
          }
        })
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/module/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.info
              }
            })
          }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
                this.$http({
                  url: this.$http.adornUrl(`/module/${!this.dataForm.id ? 'save' : 'update'}`),
                  method: 'post',
                  data: this.$http.adornData({
                    'id': this.dataForm.id || undefined,
                    'name': this.dataForm.name,
                   'backendId': this.dataForm.backendId,
                   'moduleType': this.dataForm.moduleType,
                    'serverId': this.dataForm.serverId,
                    'deployPath': this.dataForm.deployPath,


                    'databaseId': this.dataForm.databaseId,
                    'mongodbId': this.dataForm.mongodbId,
                    'redisId': this.dataForm.redisId,
                    'nameJar': this.dataForm.nameJar,

                    'dbName': this.dataForm.dbName,
                    'mondbName': this.dataForm.mondbName,
                    'redbName': this.dataForm.redbName,
                    'status': this.dataForm.status,
                  })
                }).then(({data}) => {
                  if (data && data.code === 0) {
                    this.$message({
                      message: '操作成功',
                      type: 'success',
                      duration: 1500,
                      onClose: () => {
                        this.visible = false
                        this.$emit('refreshDataList')
                      }
                    })
                  } else {
                    this.$message.error(data.msg)
                  }
                })
            }
        })
      }
    }
  }
</script>
